<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}检测设备`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="监测项目名" prop="datasetName">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写检测设备名" clearable></el-input>
      </el-form-item>

    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="DatasetDrawer">
import { ref, reactive } from "vue";
import { entrain } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import {Dataset, Device, Model, MonitioringProject} from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { getDModelList } from "@/api/modules/new2";



interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Device.ResDeviceList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  mid:any;
  did:any
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {},
  mid:"",
  did:""
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const  handleSubmit = () => {
  try {
    const mid=drawerProps.value.mid
    let data={
      mid:mid,
      name:drawerProps.value.row.name
    }
    console.log('--')
    console.log(drawerProps.value.row.did )
    if (drawerProps.value.row.did !== undefined && drawerProps.value.row.did !== null) {
      // did exists
      console.log('did exists:', drawerProps.value.row.did);
      const did=drawerProps.value.row.did
      data.did =did
    }


    drawerProps.value.api!(data);
    ElMessage.success({ message: `${drawerProps.value.title}检测设备成功！` });
    drawerProps.value.getTableList!();
    drawerVisible.value = false;
  } catch (error) {
    console.log(error);
  }


};

defineExpose({
  acceptParams
});
</script>
